package com.fgs.common.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ListView;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


/**
 *@author fgs
 *@date 2020/4/4
 *@description
 **/

public class BackToTopView extends AppCompatImageView {
    private View mRecyclerView;
    private int mVisiblePosition;

    public BackToTopView(Context context) {
        super(context);
    }

    public BackToTopView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mRecyclerView instanceof RecyclerView) {
                    ((RecyclerView) mRecyclerView).scrollToPosition(0);
                } else if (mRecyclerView instanceof ListView) {
                    ((ListView) mRecyclerView).setSelection(0);
                }
                hideBackTopView();
            }
        });
    }

    public int getVisiblePosition() {
        return mVisiblePosition;
    }

    public void setRecyclerView(RecyclerView recyclerView, int visiblePosition) {
        if (null == recyclerView) return;
        mRecyclerView = recyclerView;
        mVisiblePosition = visiblePosition;
        recyclerView.addOnScrollListener(onRecyclerViewScrollListener);
    }

    public void removeRecyclerViewListener() {
        if (null != mRecyclerView) {
            if (mRecyclerView instanceof RecyclerView) {
                ((RecyclerView) mRecyclerView).removeOnScrollListener(onRecyclerViewScrollListener);
            }
        }
    }

    private RecyclerView.OnScrollListener onRecyclerViewScrollListener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

        }

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                int firstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition();
                if (firstVisibleItem > mVisiblePosition) {
                    showBackTopView();
                } else {
                    hideBackTopView();
                }
            }
        }
    };

    private void showBackTopView(){
//       setAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.bottom_in));
        setVisibility(View.VISIBLE);

    }
    private void hideBackTopView(){
//        setAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.bottom_out));
        setVisibility(View.GONE);
    }
}
